package com.cloud.school.bus.teacherhelper.modules.home.view;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Random;

import android.content.Context;
import android.graphics.Color;
import android.text.Editable;
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.ViewFlipper;

import com.android.support.jhf.debug.DebugLog;
import com.android.support.jhf.network.NetworkClient;
import com.android.support.jhf.network.loopj.android.http.ResponseHandlerInterface;
import com.android.support.jhf.utils.InputMethodUtils;
import com.android.support.jhf.utils.ToolUtils;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.BaseLinearLayout;
import com.cloud.school.bus.teacherhelper.base.anim.swap3d.Constants;
import com.cloud.school.bus.teacherhelper.base.anim.swap3d.RotationHelper;
import com.cloud.school.bus.teacherhelper.base.dialog.CustomAlertDialog;
import com.cloud.school.bus.teacherhelper.base.utils.PictureUtil;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.entitys.LoginSetting.PhotoTag;
import com.cloud.school.bus.teacherhelper.entitys.UploadFile;
import com.cloud.school.bus.teacherhelper.modules.home.SendFileToStuActivity;
import com.cloud.school.bus.teacherhelper.modules.home.adapter.EditContentAdapter;
import com.cloud.school.bus.teacherhelper.protocol.statistics.UploadFileRequest;
import com.cloud.school.bus.teacherhelper.protocol.statistics.UploadFileResponse;

/**
 * 编辑内容
 * 
 * @author lenovo
 * 
 */
public class EditContentView extends BaseLinearLayout implements Constants {

	private EditText mContentEditText;
	private TextView mNumberTextView;
	private ViewFlipper mViewFlipper;
	private SelectedStuView mSelectedStuView;
	private TitleNavBarView mTitleNavBarView;
	private GridView mGridView;
	private TextView mAllButton;
	private EditContentAdapter mEditContentAdapter;

	private UploadFile mUploadFile;
	private List<UploadFile> mUploadFileList = new ArrayList<UploadFile>();

	private RotationHelper rotationHelper;

	// private boolean isAllFlag = false;

	public EditContentView(Context context, ViewFlipper viewFlipper) {
		super(context);
		mViewFlipper = viewFlipper;
		init();
	}

	public EditContentView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		LayoutInflater.from(mContext).inflate(R.layout.view_edit_content, this,
				true);

		mAllButton = (TextView) findViewById(R.id.allButton);

		mGridView = (GridView) findViewById(R.id.gridView);

		if (ToolUtils.isLanguage(mContext, "zh")) {
			// 系统汉语的时候
			mEditContentAdapter = new EditContentAdapter(mContext,
					mApplication.mLoginSetting.photoTagList.get(0));
		} else {
			// 系统英文的时候
			mEditContentAdapter = new EditContentAdapter(mContext,
					mApplication.mLoginSetting.photoTagList.get(1));
		}
		mEditContentAdapter.clearSelected();
		mGridView.setAdapter(mEditContentAdapter);

		mEditContentAdapter.setAllButton(mAllButton);

		rotationHelper = new RotationHelper(EditContentView.this);

		mContentEditText = (EditText) findViewById(R.id.contentEditText);
		mNumberTextView = (TextView) findViewById(R.id.numberTextView);

		setListener();
		setTitleNavBar();
	}

	/**
	 * 
	 * @param index
	 * @return 返回表示当前item是否选中
	 */
	public void setSelectedIndex(int index) {
		mEditContentAdapter.setSelectedIndex(index, mAllButton.isSelected());
		mEditContentAdapter.notifyDataSetChanged();
	}

	private void setListener() {

		mContentEditText.setFilters(new InputFilter[] { new InputFilter() {

			@Override
			public CharSequence filter(CharSequence source, int start, int end,
					Spanned dest, int dstart, int dend) {

				if (source.equals("\n")) {
					return "";
				}
				return source;
			}

		} });

		mContentEditText.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				mNumberTextView.setText(arg0.length() + "/280");
				if(null != mAllButton && !arg0.toString().equals(mUploadFile.intro)){
					mAllButton.setEnabled(true);
					mAllButton.setSelected(false);
				}
			}

			@Override
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {

			}

			@Override
			public void afterTextChanged(Editable arg0) {

			}
		});
		mGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TextView textView = (TextView)
				// arg1.findViewById(R.id.textView);
				// textView.setSelected(!textView.isSelected());
			}
		});

		mAllButton.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// mEditContentAdapter.allSelected();
				if (mAllButton.isSelected()) {
					mAllButton.setSelected(false);
				} else {
					mAllButton.setEnabled(false);
					mAllButton.setSelected(true);
				}
			}
		});
	}

	protected void setTitleNavBar() {
		mTitleNavBarView = (TitleNavBarView) findViewById(R.id.titleNavBarView);
		mTitleNavBarView.setMessage(mContext.getString(R.string.say_something));
		mTitleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// 返回
				cancel();
			}
		});
		mTitleNavBarView.setRightButtonVisibility(View.INVISIBLE);
	}

	private void cancel() {
		InputMethodUtils.hideSoftKeyboard(mContext, mContentEditText);
		getRotationHelper().applyRotation(Constants.viewA, 0, -90);
	}

	public void setSelectedStuView(SelectedStuView selectedStuView) {
		mSelectedStuView = selectedStuView;
	}

	public void setUploadFile(UploadFile uploadFile) {
		mUploadFile = uploadFile;
	}

	public void setUploadFileList(List<UploadFile> uploadFiles) {
		mUploadFileList = uploadFiles;
	}

	public EditText getContentEditText() {
		return mContentEditText;
	}

	public EditContentAdapter getEditContentAdapter() {
		return mEditContentAdapter;
	}

	@Override
	public void initSwapView() {
		// DebugLog.logI("initSwapView");
		init();
	}

	@Override
	public RotationHelper getRotationHelper() {
		// DebugLog.logI("getRotationHelper");
		return rotationHelper;
	}

	@Override
	public ViewGroup getViewGroup() {
		// DebugLog.logI("getViewGroup");
		return mViewFlipper;
	}

	@Override
	public int getSwapView() {
		// DebugLog.logI("getSwapView");
		return Constants.viewA;
	}

	@Override
	public void swapViewA() {
		// DebugLog.logI("swapViewA");
		mViewFlipper.setDisplayedChild(SendFileToStuActivity.SELECTED_STU);
		mSelectedStuView.getRotationHelper().applyRotation(Constants.viewA, 90,
				0);
		// if (mEditContentAdapter.getAllSelected()) {
		if (mAllButton.isSelected()) {
			// mUploadFile.photoTag = mContext.getString(R.string.apply_all);
			// mUploadFile.intro = mContentEditText.getText().toString();
			// mSelectedStuView.setContent(mContext.getString(R.string.apply_all),
			// mContentEditText.getText().toString());
			int photoIndex = -1;
			String photoTag = "";
			photoIndex = mEditContentAdapter.getCurrentItem();
			if (-1 != photoIndex) {
				photoTag = ((PhotoTag) mEditContentAdapter.getItem(photoIndex)).photoTag;
			}

			for (int i = 0; i < mUploadFileList.size(); i++) {
				UploadFile uploadFile = mUploadFileList.get(i);
				uploadFile.photoTag = photoTag;
				uploadFile.intro = mContentEditText.getText().toString();
			}
			mSelectedStuView.setContent(photoTag, mContentEditText.getText()
					.toString());
		} else {
			int photoIndex = -1;
			String photoTag = "";
			photoIndex = mEditContentAdapter.getCurrentItem();
			if (-1 != photoIndex) {
				photoTag = ((PhotoTag) mEditContentAdapter.getItem(photoIndex)).photoTag;
			}
			mUploadFile.photoTag = photoTag;
			mUploadFile.intro = mContentEditText.getText().toString();
			mSelectedStuView.setContent(photoTag, mContentEditText.getText()
					.toString());
		}
	}

	@Override
	public void swapViewB() {
		// DebugLog.logI("swapViewB");
		mViewFlipper.setDisplayedChild(SendFileToStuActivity.EDIT_CONTENT);
	}
}
